/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client;



import net.bioteam.appweb.gwt.apps.client.dataanalysis.DataInputPanel;
import net.bioteam.appweb.gwt.apps.client.dataanalysis.ServicePanel;
import net.bioteam.appweb.gwt.apps.client.datatree.DataTreeGrid;
import net.bioteam.appweb.gwt.apps.client.ds.DatasetPanel;
import net.bioteam.appweb.gwt.apps.client.program.ProgramFormLoader;
import net.bioteam.appweb.gwt.apps.client.user.AccountPanel;
import net.bioteam.appweb.gwt.apps.client.user.JobPanel;

import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.Window;

public class ApplicationHistoryListener implements HistoryListener
{

	public void onHistoryChanged(String historyToken)
	{
		if (!LoginHolder.isLogined())
		{
			//go to the login panel
			//StaticHolder.loginPanel.setToken(historyToken);
			return;
		}
		String[] tokens=historyToken.split("_");
		if (tokens[0].equals("appnode"))
		{
			handleAppNode(tokens);
		} else if (tokens[0].equals("user"))
		{
			handleUser(tokens);
		}else if (tokens[0].equals("admin"))
		{
			handleAdmin(tokens);
		}
		
	}
	
	private void handleAdmin(String[] tokens)
	{
		if (tokens[1].equals("users"))
		{		
			Window.setTitle("User Accounts");
			StaticHolder.content.switchPanel(StaticHolder.getUsersPanel());
			StaticHolder.getUsersGrid().load();
		} else if (tokens[1].equals("ds"))
		{		
			Window.setTitle("Dataset Management");
			
			if (StaticHolder.datasetPanel==null)
			{
				StaticHolder.datasetPanel=new DatasetPanel();
				StaticHolder.content.addChild(StaticHolder.datasetPanel);
			}
			if (tokens.length==3 && tokens[2].endsWith("local"))
			{
				StaticHolder.datasetPanel.ts.selectTab(1);
			} else
			{
				StaticHolder.datasetPanel.ts.selectTab(0);
			}
			StaticHolder.content.switchPanel(StaticHolder.datasetPanel);
			
		}
		
	}
	
	
	private void handleUser(String[] tokens)
	{
		
		if (tokens[1].equals("templates"))
		{
			Window.setTitle("Application templates");
			StaticHolder.content.switchPanel(StaticHolder.getTemplatePanel());
			StaticHolder.getTemplateGrid().load();
		}  else if (tokens[1].equals("qstat"))
		{
			Window.setTitle("SGE queue status");
			StaticHolder.content.switchPanel(StaticHolder.getQstat());
			StaticHolder.getQstat().load();
		}
		else if (tokens[1].equals("jobs"))
		{
			JobPanel jPanel=StaticHolder.jobPanel;
			DataTreeGrid dataTree=StaticHolder.getDataTreeGrid();
			
			if (!jPanel.getFileLayout().contains(dataTree))
			{
				if (StaticHolder.content.contains(dataTree))
				{
					StaticHolder.content.removeChild(dataTree);
				}
				jPanel.getFileLayout().addMember(dataTree);
				StaticHolder.content.switchPanel(StaticHolder.jobPanel);
				dataTree.show();
			} else
			{
				StaticHolder.content.switchPanel(StaticHolder.jobPanel);
			}
			if (tokens.length==3)
			{
				Window.setTitle("Job Detail");
				StaticHolder.jobDetailGrid.reload(Long.parseLong(tokens[2]));
				jPanel.getTs().selectTab(1);
				
			}  else
			{
				Window.setTitle("Job History");
				StaticHolder.jobGrid.load();
				jPanel.getTs().selectTab(0);
			}
			
			
			//StaticHolder.content.switchPanel(StaticHolder.jobPanel);	
			
			
			
		} else if (tokens[1].equals("account"))
		{
			if (tokens.length==3)
				AccountPanel.showAccount(tokens[2]);
			else
				AccountPanel.showAccount(null);
		}else if (tokens[1].equals("data"))
		{
			Window.setTitle("My Data in server");
			DataTreeGrid dataTree=StaticHolder.getDataTreeGrid();
			if (StaticHolder.jobPanel.getFileLayout().contains(dataTree))
			{
				StaticHolder.jobPanel.getFileLayout().removeMember(dataTree);
			}
			if (!StaticHolder.content.contains(dataTree))
			{
				StaticHolder.content.addMember(dataTree);
			}
			StaticHolder.content.switchPanel(dataTree);
		}
		else if (tokens[1].equals("analysis"))
		{
			handleAnalysis(tokens);
		}
	}
	
	private void handleAnalysis(String[] tokens)
	{
		if (tokens.length==2 || (tokens.length==3 && tokens[2].equals("1")))
		{
			Window.setTitle("Data-based analysis:data Input");
			DataInputPanel dp=StaticHolder.getDataInputPanel();
			StaticHolder.content.switchPanel(dp);
		} else if (tokens[2].equals("2"))
		{
			Window.setTitle("Data-based analysis: analysis selection");
			ServicePanel dp=StaticHolder.getServicePanel();
			StaticHolder.content.switchPanel(dp);
			StaticHolder.servicesGrid.reload();
		}
	}
	
	private void handleAppNode(String[] tokens)
	{
		if (tokens.length<3)
		{
			return;
		}
		
		String ptype=tokens[1];
		String pvalue=null;
		for (int i=2; i<tokens.length; i++)
		{
			if (pvalue!=null)
			{
				pvalue+="_"+tokens[i];
			} else
			{
				pvalue=tokens[i];
			}
			
		}
		new ProgramFormLoader(ptype,pvalue).execute();
	}

}
